<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <!-- <el-form-item label="部门id" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入部门id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->

      <el-form-item label="部门" prop="deptId">
        <treeselect style="width: 240px" clearable v-model="queryParams.deptId" :options="deptOptions" :show-count="true"
          placeholder="请选择归属部门" />
      </el-form-item>

      <el-form-item label="教师工号" prop="teacherNo">
        <el-input v-model="queryParams.teacherNo" placeholder="请输入教师工号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="真实姓名" prop="realName">
        <el-input v-model="queryParams.realName" placeholder="请输入真实姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-input v-model="queryParams.gender" placeholder="请输入性别" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="评教成绩" prop="evaluateScore">
        <el-input v-model="queryParams.evaluateScore" placeholder="请输入评教成绩" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="评教状态" prop="evaluateState">
        <el-input v-model="queryParams.evaluateState" placeholder="请输入评教状态" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="职称" prop="academicTitle">
        <el-input v-model="queryParams.academicTitle" placeholder="请输入职称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="1内部，0外聘" prop="internal">
        <el-input v-model="queryParams.internal" placeholder="请输入是否外聘；1表示内部，0表示外聘教师" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="用户id" prop="userId">
        <el-input v-model="queryParams.userId" placeholder="请输入用户id" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['niuyabo:teacher:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['niuyabo:teacher:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['niuyabo:teacher:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['niuyabo:teacher:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="teacherList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="教师id" align="center" prop="teacherId" />
      <!-- <el-table-column label="部门id" align="center" prop="deptId" /> -->
      <el-table-column label="教师工号" align="center" prop="teacherNo" />
      <el-table-column label="真实姓名" align="center" prop="realName" />
      <el-table-column label="部门id" align="center" prop="dept.deptName" />
      <el-table-column label="职称" align="center" prop="academicTitle" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="性别" align="center" prop="gender" />
      <el-table-column label="评教成绩" align="center" prop="evaluateScore" />
      <el-table-column label="评教状态" align="center" prop="evaluateState" />
      <el-table-column label="1内部，0外聘" align="center" prop="internal" />
      <el-table-column label="用户id" align="center" prop="userId" />
      <!-- <el-table-column label="用户名" align="center" prop="userId" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['niuyabo:teacher:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['niuyabo:teacher:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改教师管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <!-- <el-form-item label="部门id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门id" />
        </el-form-item> -->

        <el-form-item label="教师用户" prop="userId">
          <el-select v-model="form.userId" filterable placeholder="请绑定教师用户" style="width: 150px">
            <el-option v-for="item in userList" :key="item.userId" :label="item.userName"
              :value="item.userId" :disabled="item.status == 1"></el-option>
          </el-select>
          用户id：<el-input v-model="form.userId" disabled  placeholder="请输入用户id" style="width: 100px" />
        </el-form-item>

        <el-form-item label="归属部门" prop="deptId">
          <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属部门" />
        </el-form-item>
        <!-- <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item> -->
        <el-form-item label="教师工号" prop="teacherNo">
          <el-input v-model="form.teacherNo" placeholder="请输入教师工号" />
        </el-form-item>
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入真实姓名" />
        </el-form-item>
        <!-- <el-form-item label="性别" prop="gender">
          <el-input v-model="form.gender" placeholder="请输入性别" />
        </el-form-item> -->
        <el-form-item label="评教成绩" prop="evaluateScore">
          <el-input v-model="form.evaluateScore" placeholder="请输入评教成绩" />
        </el-form-item>
        <el-form-item label="评教状态" prop="evaluateState">
          <el-input v-model="form.evaluateState" placeholder="请输入评教状态" />
        </el-form-item>
        <el-form-item label="职称" prop="academicTitle">
          <el-input v-model="form.academicTitle" placeholder="请输入职称" />
        </el-form-item>
        <el-form-item label="1内部，0外聘" prop="internal">
          <el-input v-model="form.internal" placeholder="请输入是否外聘；1表示内部，0表示外聘教师" />
        </el-form-item>

        <!-- <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item> -->

        <el-divider content-position="center">教师上课信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddCourseclassTeacher">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteCourseclassTeacher">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="courseclassTeacherList" :row-class-name="rowCourseclassTeacherIndex" @selection-change="handleCourseclassTeacherSelectionChange" ref="courseclassTeacher">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>

            <!-- <el-input v-model="queryParams.courseclassId" placeholder="请输入上课班级" clearable @keyup.enter.native="handleQuery" /> -->

          <!-- <el-form-item label="上课班级" prop="courseclassId">
            <el-select v-model="form.courseclassId" filterable placeholder="请选择上课班级">
              <el-option v-for="item in courseclassList" :key="item.courseclassId" :label="item.courseclassNo"
                :value="item.courseclassId" :disabled="item.status == 1"></el-option>
            </el-select>
          </el-form-item> -->

          <el-table-column label="上课班级" prop="courseclassId" width="150px">
            <template slot-scope="scope">
              <!-- <el-input v-model="scope.row.courseclassId" placeholder="请选择上课班级" /> -->
              <el-select v-model="scope.row.courseclassId" filterable placeholder="请选择上课班级">
                <el-option v-for="item in courseclassList" :key="item.courseclassId" :label="item.courseclassNo"
                  :value="item.courseclassId" :disabled="item.status == 1"></el-option>
              </el-select>
            </template>
          </el-table-column>

          <el-table-column label="上课班级名" prop="course.courseName" width="120px">
            <template slot-scope="scope">
              <!-- <el-input v-model="scope.row.courseclassId" placeholder="请选择上课班级" /> -->
              <el-select v-model="scope.row.courseclassId" filterable placeholder="请选择上课班级名">
                <el-option v-for="item in courseclassList" :key="item.courseclassId" :label="item.course.courseName"
                  :value="item.courseclassId" :disabled="item.status == 1"></el-option>
              </el-select>
            </template>
          </el-table-column>


          <el-table-column label="评价成绩" prop="evaluationScore" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.evaluationScore" placeholder="请输入评价成绩" />
            </template>
          </el-table-column>
        </el-table>
        

        

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTeacher, getTeacher, delTeacher, addTeacher, updateTeacher } from "@/api/score/teacher";
import { deptTreeSelect } from "@/api/system/user";

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

import { allocatedUserList } from "@/api/system/role";

import { listCourseclass } from "@/api/score/courseclass";

export default {
  name: "Teacher",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
       // 子表选中数据
       checkedCourseclassTeacher: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教师管理表格数据
      teacherList: [],
      // 教师上课表格数据
      courseclassTeacherList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 部门树选项
      deptOptions: [],
      // 教师用户表格数据
      userList: [],
      // 课程班级表格数据
      courseclassList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        status: null,
        teacherNo: null,
        realName: null,
        gender: null,
        evaluateScore: null,
        evaluateState: null,
        academicTitle: null,
        internal: null,
        userId: null,

        roleId: 102
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    /** 查询教师管理列表 */
    getList() {
      this.loading = true;
      listTeacher(this.queryParams).then(response => {
        this.teacherList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      /** 查询授权教师用户列表 */
      allocatedUserList(this.queryParams).then(response => {
        this.userList = response.rows;
        // this.total = response.total;
        // this.loading = false;
      });
       /** 查询课程班级列表 */
       listCourseclass().then(response => {
        this.courseclassList = response.rows;
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        teacherId: null,
        deptId: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        teacherNo: null,
        realName: null,
        gender: null,
        evaluateScore: null,
        evaluateState: null,
        academicTitle: null,
        internal: null,
        userId: null
      };
      this.courseclassTeacherList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.teacherId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加教师管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const teacherId = row.teacherId || this.ids
      getTeacher(teacherId).then(response => {
        this.form = response.data;
        this.courseclassTeacherList = response.data.courseclassTeacherList;
        this.open = true;
        this.title = "修改教师管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.courseclassTeacherList = this.courseclassTeacherList;
          if (this.form.teacherId != null) {
            updateTeacher(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTeacher(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const teacherIds = row.teacherId || this.ids;
      this.$modal.confirm('是否确认删除教师管理编号为"' + teacherIds + '"的数据项？').then(function () {
        return delTeacher(teacherIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    	/** 教师上课序号 */
      rowCourseclassTeacherIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 教师上课添加按钮操作 */
    handleAddCourseclassTeacher() {
      let obj = {};
      obj.evaluationScore = "";
      this.courseclassTeacherList.push(obj);
    },
    /** 教师上课删除按钮操作 */
    handleDeleteCourseclassTeacher() {
      if (this.checkedCourseclassTeacher.length == 0) {
        this.$modal.msgError("请先选择要删除的教师上课数据");
      } else {
        const courseclassTeacherList = this.courseclassTeacherList;
        const checkedCourseclassTeacher = this.checkedCourseclassTeacher;
        this.courseclassTeacherList = courseclassTeacherList.filter(function(item) {
          return checkedCourseclassTeacher.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleCourseclassTeacherSelectionChange(selection) {
      this.checkedCourseclassTeacher = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('niuyabo/teacher/export', {
        ...this.queryParams
      }, `teacher_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
